package com.controller;

import com.base.*;
import com.bean.WebsiteDictData;
import com.bean.anno.LogAnno;
import com.github.pagehelper.PageHelper;
import com.mapper.mysql.WebsiteDictDataMapper;
import com.mapper.repository.mysql.WebsiteDictDataRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("websiteType")
public class WebsiteDictDataController extends BaseController {

    @Autowired
    private WebsiteDictDataMapper websiteDictDataMapper;

    @Autowired
    private WebsiteDictDataRepository websiteTypeRepository;

    @LogAnno(apiName = "获取网址类型")
    @PostMapping("list")
    public AjaxResult list(@RequestBody AjaxRequest<WebsiteDictData> request){
        WebsiteDictData websiteType = request.getT();
        MetaInfo metaInfo = request.getMetaInfo();
        PageHelper.startPage(metaInfo.getPage(),metaInfo.getSize());
        List<WebsiteDictData> list = websiteDictDataMapper.select(websiteType);
        return success("查询成功",list,metaInfo);
    }


    @LogAnno(apiName = "添加网址类型")
    @PostMapping("add")
    public AjaxResult add(@RequestBody WebsiteDictData websiteType){
        String id = UUID.randomUUID().toString();
        websiteType.setId(id);
        websiteType.setCreatedate(LocalDate.now());
        websiteTypeRepository.save(websiteType);
        return success("新增成功");
    }

    @LogAnno(apiName = "删除网址类型")
    @PostMapping("del")
    public AjaxResult del(@RequestBody WebsiteDictData websiteType){
        websiteTypeRepository.delete(websiteType);
        return success("删除成功");
    }

    @LogAnno(apiName = "修改网址类型")
    @PostMapping("update")
    public AjaxResult update(@RequestBody WebsiteDictData websiteType){
        websiteType.setUpdatedate(LocalDate.now());
        websiteTypeRepository.save(websiteType);
        return success("更新成功");
    }

    @LogAnno(apiName = "添加网址类型下拉选")
    @PostMapping("selectInfo")
    public AjaxResult selectInfo(@RequestBody SelectInfo selectInfo){
        List<SelectInfo> list = websiteDictDataMapper.types(selectInfo.getValue());
        return success("查询成功",list);
    }


    @LogAnno(apiName = "获取网址大类下拉选")
    @GetMapping("categorys/selectInfo")
    public AjaxResult categorys(){
        List<SelectInfo> list = websiteDictDataMapper.categorys();
        return success("查询成功",list);
    }

    @LogAnno(apiName = "添加网址类型")
    @PostMapping("categorys/add")
    public AjaxResult categorysAdd(@RequestBody WebsiteDictData websiteType){
        String id = UUID.randomUUID().toString();
        websiteType.setId(id);
        websiteType.setCreatedate(LocalDate.now());
        websiteTypeRepository.save(websiteType);
        return success("新增成功");
    }



}
